<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form ref="form" :model="form" :rules="rules" >
    <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading">
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="code"/>
      <el-table-column align="center" label="产品名称" prop="goodsName"/>
      <el-table-column align="center" label="产品型号" prop="model"/>
      <el-table-column align="center" label="产品明细" prop="brand"/>
      <el-table-column align="center" label="产品单位" prop="unit"/>
      <el-table-column align="center" label="订单单价" prop="goodsUnitPrice"/>
      <el-table-column align="center" label="锁定数量" prop="goodsCount"/>
      <el-table-column align="center" label="订单数量" prop="counts"/>
      <el-table-column label="是否自制" align="center" prop="isdo">
        <template slot-scope="scope">
          <!--<dict-tag :options="dict.type.yes_no" :value="scope.row.isdo"/>-->
          <el-select v-model="scope.row.isdo"  placeholder="是否自制" clearable >
            <el-option v-for="dict in dict.type.isdo"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="(请购/自制)数量" align="center" prop="shengyu" width="210">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.shengyu" :min="0"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="210">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark"></el-input>
        </template>
      </el-table-column>
</el-table>
    </el-form>
    <div class="dialog-footer" slot="footer">
        <el-button type="primary" @click="submitBuyForm" v-if="this.status==2" >发起请购</el-button>
        <el-button @click="cancel">取 消</el-button>
    </div>
    <goods-orderin-chose @ok="refreshPageData" ref="formChose"></goods-orderin-chose>
    <goodSale-form @ok="getList" ref="goodSaleForm"></goodSale-form>
  </el-dialog>
</template>


<script>

    import {addAction,deleteAction, getAction, getById} from '@/api/manage'
    import GoodsOrderinChose from "./PleaseBuyOrderinChose";
    import GoodSaleForm from "./GoodSaleForm";

    export default {
        name: "PleaseBuyFormGoods2",
        dicts: [ 'isdo'],
        components: {
            GoodsOrderinChose,
            GoodSaleForm
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                proId: "",
                status: "",
                columns: [],
                total: 0,
                // 表单校验
                rules: {
                },
                queryParams: {
                    pageNum: 1,
                    pageSize: 1000,
                    id: null,
                    name: null,
                    taxNo: null,
                    addr: null,
                    tel: null,
                    bankName: null,
                    bankNo: null,
                    info: null,
                    status: "2",
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null,
                    type: null,
                },
                url: {
                    list: '/goodsSale/goodsSale/listSaleProject',
                    listBatch: '/goodsSale/goodsSale/listBatchSale/',
                    getById: '/projectBaseInfo/projectBaseInfo/',
                    add: '/projectBaseInfo/projectBaseInfo/add',
                    edit: '/projectBaseInfo/projectBaseInfo/edit',
                    remove: '/goodsSale/goodsSale/deleteProject/',
                    pleaseBuyForPrj: '/goodsSaledo/goodsSaledo/pleaseBuyForPrj',
                },
            };
        },
        created() {

        },
        methods: {
            //重新计算利润,因部分旧数据未参与计算 : 商品销售金额为新增加字段
            refreshProfit() {
                let that = this;
                let params = {id: this.proId};
                getAction("/projectBaseInfo/projectBaseInfo/refreshProfit", params).then(response => {
                    that.refreshPageData();
                });
            },
            refreshPageData() {
                let that = this;
                let param = {
                    id: that.proId
                }
                that.handleUpdate(param);
            },
            /*handleChangeMoney(row) {
                let that = this;
                let handleReckonMoney = (row) => {
                    let row_money = 0;
                    row_money = that.functionNumberMul(Number(row.goodsUnitPrice), Number(row.goodsCount))
                    that.$set(row, 'goodsTotalPrice', row_money);
                };
                handleReckonMoney(row);
            },*/
            /*functionNumberMul(arg1, arg2) {
                var m = 0;
                var s1 = arg1.toString();
                var s2 = arg2.toString();
                try {
                    m += s1.split(".")[1].length;
                } catch (e) {
                }
                try {
                    m += s2.split(".")[1].length;
                } catch (e) {
                }
                return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
            },*/
            handleChose() {
                this.$refs.formChose.chose(this.proId, "2");
            },
            getList() {
                this.loading = true;
                /*getById(this.url.listBatch, this.ids).then(response => {
                  this.columns = response.rows;
                  this.total = response.total;
                  this.loading = false;
                });*/
                this.queryParams.projectId = this.proId
                getAction(this.url.list, this.queryParams).then(response => {
                    this.columns = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.refreshPageData();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null,
                    name: null,
                    master: null,
                    customId: null,
                    customName: null,
                    companyName: null,
                    contractDate: null,
                    contractMoney: null,
                    auditMoney: null,
                    checkDate: null,
                    checkState: null,
                    fundCallback: null,
                    infomation: null,
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            /*handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加项目基础信息管理";
            },*/
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                this.proId = id;
                this.status=row.status;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改订单基础信息管理";
                    this.getList();
                });
            },
          handleUpdateSale(row) {
            this.$refs.goodSaleForm.handleUpdateShow(row);
            },
            /** 提交按钮 */
            submitBuyForm() {
              alert("请购成功");
                let that = this;
                that.$refs["form"].validate(valid => {
                    if (valid) {
                      const goodsOrderIn = Object.assign({});
                      goodsOrderIn.projectId = this.proId;
                      goodsOrderIn.proStatus = this.status;
                      goodsOrderIn.type = '2';
                      goodsOrderIn.columns = that._data.columns;
                      addAction(this.url.pleaseBuyForPrj, goodsOrderIn).then(response => {
                        this.$modal.msgSuccess("请购成功");
                        this.open = false;
                        this.$emit('ok');
                      });


                    }
                });
            },
        }
    };
</script>
